package com.example.book.service;

import com.example.book.mapper.BookDaoMapper;
import com.example.book.model.Book;
import com.example.book.model.Member;
import com.example.book.model.Order;
import com.example.book.model.OrderBookUnion;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.*;

@Service
public class BookServiceServiceImpl implements BookService {

    @Resource
    BookDaoMapper bookDaoMapper;


    @Override
    public void addMember(Member member) {


         bookDaoMapper.addMember(member);
    }

    /**
     * 提交订单 添加书本 和订单关联关系表orderBookUnion
     *
     * @param order
     */
    @Override
    @Transactional
    public void insertOrder(Order order) {
        String id=order.getMemberId();

        Member member = bookDaoMapper.qryMember(id);


        String[] bookId = order.getBookId();
        List<Book> books = bookDaoMapper.qryBooks(bookId);
        // 收集订单总价
        double total=0.00;
        for (Book book:books) {
            OrderBookUnion orderBookUnion = new OrderBookUnion();
            orderBookUnion.setBookId(book.getBookId());
            orderBookUnion.setOrderId(order.getOrderId());
            // 初始状态
            orderBookUnion.setFlag(0);
            orderBookUnion.setTime(new Date());
            Double price = book.getPrice();
            total=total+price;
            bookDaoMapper.insertOrderBookUnion(orderBookUnion);

        }
        // 更新积分
        if(member!=null){
            double oldsum = order.getSum();
            double totalsum = oldsum+total;
            order.setSum(totalsum);
            // 积分根据会员级别 核算
            double scored = totalsum * member.getType();
            double oldscore = member.getScore();
            double sumscore = oldscore+scored;
            member.setScore(sumscore);
            bookDaoMapper.updateMemberScoreById(member);

        }
        // 插入订单
        bookDaoMapper.insertOrder(order);
    }

    @Override
    public List<Order> qryOrders(Map<String, String> map) {
        List<Order> orders = bookDaoMapper.qryOrders(map);

        for (Order order:orders) {
            String orderId = order.getOrderId();
            List<OrderBookUnion> list = bookDaoMapper.qryOrderBookUnions(orderId);
            List<String> strings = new ArrayList<>();
            for (OrderBookUnion obk:list) {
                String bookId = obk.getBookId();
                strings.add(bookId);
            }
            Object[] bookIds = strings.toArray();
            String[] booksIds = new String[bookIds.length];
            int index=0;
            for (Object obj: bookIds) {
                booksIds[index] =(String)obj;
                index++;
            }
            order.setBookId(booksIds);
        }
        return orders;
    }

    @Override
    public List<Book> qryBook(String bookId) {
        return bookDaoMapper.qryBook(bookId);
    }

    @Override
    public List<Book> qryBooks(String[] bookId) {
        return bookDaoMapper.qryBooks(bookId);
    }
}
